﻿using GalaSoft.MvvmLight;
using Cleverscape.UTorrentClient.WebClient;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace uTorrentSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class FiltersViewModel : MyBaseViewModel
    {
        public RelayCommand<Filter> SelectCommand { get; private set; }

        public RelayCommand<string> AddFilterCommand { get; set; }
       

        /// <summary>
        /// The <see cref="SelectedFilter" /> property's name.
        /// </summary>
        public const string SelectedFilterPropertyName = "SelectedFilter";

        private Cleverscape.UTorrentClient.WebClient.Filter _selectedFilter = null;

        /// <summary>
        /// Gets the SelectedFilter property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Cleverscape.UTorrentClient.WebClient.Filter SelectedFilter
        {
            get
            {
                return _selectedFilter;
            }

            set
            {
                if (_selectedFilter == value)
                {
                    return;
                }
                
                var oldValue = _selectedFilter;
                _selectedFilter = value;

                // Remove one of the two calls below
                //throw new NotImplementedException();

                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedFilterPropertyName);

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                //RaisePropertyChanged(SelectedFilterPropertyName, oldValue, value, true);
            }
        }
        /// <summary>
        /// Initializes a new instance of the FiltersViewModel class.
        /// </summary>
        public FiltersViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
            SelectCommand = new RelayCommand<Filter>(f =>
            {
                Messenger.Default.Send<string>("FilterView", "LoadView");
                Messenger.Default.Send<Filter>(f, "SetFilter");
            });

            AddFilterCommand = new RelayCommand<string>((name) =>
            {
                Filter newFilter = new Filter();
                newFilter.Name = name;
                //this.Data.FilterUpdateAdd(newFilter);
                ViewModelLocator.CloseModal();
                SelectCommand.Execute(newFilter);
            });
            
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
    }
}